areas.levelEditor = function()
	areas.current = "levelEditor"

	
	  --table.insert(dynamics, p)
	--p.damping = 5
	--p.friction = 0.15
	--[[p.onCollide = function(p, b, nx, ny, pen)
		if ny < 0 then
			p.grounded = true
			p.jumptime = nil
		end
		return true
	end]]

	hook.Add("draw", "levelEditorUI", levelEditorUI)
end

local w = ScrW()
local h = ScrH()
editor.tools.current = "none"
local sideBarWidth = 50
local topBarWidth = 250
local topBarHeight = 50
local spacer = 5
local newX = w/2-topBarWidth/2
local newY = topBarHeight-spacer*2

function levelEditorUI()

	drawEdges(level.curBaseGrid)

	love.graphics.setColor(255,255,255,20)
	love.graphics.rectangle("fill", 0, 0, sideBarWidth, h )
	love.graphics.rectangle("fill", w-sideBarWidth, 0, sideBarWidth, h )

	love.graphics.setColor(255,255,255,255)
	gui.EasyImage(iDB.UI.leftArrow, (sideBarWidth/2)-iDB.UI.leftArrow:getWidth()/2, h/2-iDB.UI.leftArrow:getHeight()/2, iDB.UI.leftArrow:getWidth(), iDB.UI.leftArrow:getHeight())
	gui.EasyImage(iDB.UI.rightArrow, w-(sideBarWidth/2)-iDB.UI.rightArrow:getWidth()/2, h/2-iDB.UI.rightArrow:getHeight()/2, iDB.UI.rightArrow:getWidth(), iDB.UI.rightArrow:getHeight())
	

	gui.ImageButton("", {sideBarWidth,h}, {0,0}, iDB.blank, false, function()

	end)

	gui.ImageButton("", {sideBarWidth,h}, {w-sideBarWidth,0}, iDB.blank, false, function()

	end)

	love.graphics.setColor(255,255,255,30)


	love.graphics.rectangle("fill", newX, 0, topBarWidth, topBarHeight )
	table.insert(gui.layer,{newX,0,topBarWidth,topBarHeight})
	local indent = 3
	local r_indent = indent + 1.5
	local r = newY/2-indent

	love.graphics.rectangle("fill", newX + spacer+r_indent-0.5, spacer+newY/2 -(newY-r_indent*2)/2, newY-r_indent*2,newY-r_indent*2)
	love.graphics.rectangle("line", newX + spacer+r_indent+0.5, spacer+newY/2 -(newY-r_indent*2)/2, newY-r_indent*2,newY-r_indent*2)

	love.graphics.circle("fill", newX+ newY+ spacer*2 + newY / 2- 0.5, spacer+newY/2 - 0.5, r,15)
	love.graphics.circle("line", newX+ newY+ spacer*2 + newY / 2 - 0.5, spacer+newY/2 - 0.5, r,15)

	
	love.graphics.setColor(255,0,0,255)
	if editor.tools.current == "rectangle" then
		love.graphics.rectangle("line", newX+spacer, spacer, newY,newY)
	elseif editor.tools.current == "circle" then
		love.graphics.rectangle("line", newX+newY+spacer*2, spacer, newY,newY)
	end

	gui.ImageButton("", {newY, newY}, {newX+spacer,spacer}, iDB.blank, false, function()
		editor.tools.current = "rectangle"
	end)

	gui.ImageButton("", {newY, newY}, {newX+newY+spacer*2,spacer}, iDB.blank, false, function()
		editor.tools.current = "circle"
	end)

	gui.ImageButton(">", {newY, newY}, {newX+newY*2+spacer*3,spacer}, iDB.blank, false, function()
		editor.testLevel()
	end)

	gui.ImageButton("S", {newY, newY}, {newX+newY*3+spacer*4,spacer}, iDB.blank, false, function()
		level.saveMap()
	end)

	gui.ImageButton("L", {newY, newY}, {newX+newY*4+spacer*5,spacer}, iDB.blank, false, function()
		level.loadMap()
	end)
end




local mStore = {1,1}
local depressed = false

function levelEditorMousePress(args)
	button = args[3]
	if not mouseIsTouchingLayer(gui.layer) and button=="l" and areas.current == "levelEditor" then 
		mStore = {love.mouse.getX(), love.mouse.getY()}
		depressed = true
		--popupText_spawn("gay", "description?", "click")
	end


end

local gridSize = 57

local sides
local ei = gridSize/16

local dirtImage = love.graphics.newImage("resources/images/terrain/dirt.png")

function grabEdges(tbl)

	-- sides are bool in the following formation

	-- [1] [2] [3]
	-- [8] [-] [4]
	-- [7] [6] [5]

	-- if sides[n] then sides[n] is another block
	--print(i)


	local x2 = gridSize*tbl[1]
	local y2 = gridSize*tbl[2]
	local ri = x2+gridSize
	local bo = y2+gridSize

	local scale = 1 + 1/10

	local sides = level.curBaseGrid[tbl[1]][tbl[2]].sides

	-- sides are bool in the following formation

	-- [1] [2] [3]
	-- [8] [-] [4]
	-- [7] [6] [5]

	-- if sides[n] then sides[n] is another block

	--scalex2 = scale - scale/(indentp/2)

		local imgH = dirtImage:getHeight()
		local imgW = dirtImage:getWidth()
		local scaledif = (imgW*scale - imgW)
		y2 = y2 - imgH / 2
		x2 = x2 - scaledif / 2
		local w = imgW+scaledif

	local IXYWHR = {}

	if not sides[2] then
		addKnownEdge({dirtImage, x2,y2,w,imgH, 0})
	end
	
	if not sides[4] then
		addKnownEdge({dirtImage, x2+w,y2,w,imgH,90})
	end

	if not sides[6] then
		addKnownEdge({dirtImage, x2+w,y2+w,w,imgH,180})
	end

	if not sides[8] then
		addKnownEdge({dirtImage, x2,y2+w,w,imgH,270})
	end
end

function addKnownEdge(IXYWHR)
	if not table.HasValue(level.curBaseGrid.knownEdges, IXYWHR) then
		table.insert(level.curBaseGrid.knownEdges, IXYWHR)
	end
end



function drawEdges(map)
	if not level.curBaseGrid.knownEdges then level.curBaseGrid.knownEdges = {} end
	for i,v in pairs(level.curBaseGrid.knownEdges) do

		love.graphics.setColor(255,255,255,255)
		--local preMath = 
		--print(preMath)
		love.graphics.draw(v[1], v[2], v[3], math.rad(v[6]), 1, 1)
		--gui.EasyImage(v[1], v[2],v[3],v[4],v[5],false,v[6])
	end
end

local function squareExists(point, map)
	if not map then return false
	elseif not map[point[1]] then return false
	elseif not map[point[1]][point[2]] then return false
	elseif not map[point[1]][point[2]].type then return false
	else return true
	end
end

function checkSides( map )
	for i,v in pairs(map) do
		if type(i)~= "string" then
			for j, k in pairs(v) do
				local x = i
				local y = j
				local checkTable = {{i-1,y-1},{i,y-1},{i+1,y-1},{i+1,y},{i+1,y+1},{i,y+1},{i-1,y+1},{i-1,y}}
				-- LOL ^(^_^)^ WHAT AM I EVEN DOING
				local returnTable = {}
				for n,o in pairs(checkTable) do
					if not squareExists({o[1],o[2]},map) then returnTable[n] = false 
					else returnTable[n] = true end
				end
				k.sides = returnTable
				grabEdges({i,j})
			end
		end
	end
end

local loc = "maps.txt"

function level.saveMap()
	level.maps.autosave = level.curBaseGrid
	table.save(level.maps, loc)
end

function level.loadMap()
	level.clearMap()
	statics = {}
	level.maps = table.load( loc )
	local map = level.maps.autosave

	for i,v in pairs(map) do
		if type(i)~="string" then
			for j, k in pairs(v) do
				local p = fizz.addStatic('rect', (i*gridSize)+gridSize/2, (j*gridSize)+gridSize/2, gridSize, gridSize)
				if not table.HasValue(statics, p) then
					table.insert(statics, p) 
				end
			end
		end
	end
	--local p = fizz.addStatic('rect', gridAlignX(love.mouse.getX())+gridSize/2, gridAlignY(love.mouse.getY())+gridSize/2, gridSize, gridSize)
	--table.insert(statics, p)
end

function level.refreshMap()
	level.clearMap()
	statics = {}
	level.maps = table.load( loc )
	local map = level.maps.autosave


	for i,v in pairs(map) do
		for j, k in pairs(v) do
			local p = fizz.addStatic('rect', (i*gridSize)+gridSize/2, (j*gridSize)+gridSize/2, gridSize, gridSize)
			if not table.HasValue(statics, p) then
				table.insert(statics, p) 
			end
		end
	end
	--local p = fizz.addStatic('rect', gridAlignX(love.mouse.getX())+gridSize/2, gridAlignY(love.mouse.getY())+gridSize/2, gridSize, gridSize)
	--table.insert(statics, p)
end

function level.clearMap()
	for i,v in pairs(statics) do
		fizz.removeShape(v)
	end
end

local function getMouseGrid()
	local x = love.mouse.getX()
	local y = love.mouse.getY()

	x = (x - (x % gridSize)) / gridSize
	y = (y - (y % gridSize)) / gridSize

	return {x,y}
end

local function gridAlignX(x)
	--print(x-x%gridSize)
	--print(x+x%gridSize)
	x = x - gridSize / 2
	if x % gridSize <= gridSize / 2 then
		return x - (x % gridSize)-- - gridSize / 2 
	else
		return x + (gridSize - (x % gridSize))-- - gridSize / 2 
	end
end

local function gridAlignY(y)
	y = y - gridSize / 2
	if y % gridSize <= gridSize / 2 then
		return y - (y % gridSize)-- - gridSize / 2 
	else
		return y + (gridSize - (y % gridSize))-- - gridSize / 2 
	end
end

function levelEditorMouseRelease(args)

	

	level.saveMap()
	level.loadMap()

	local button = args[3]
	
	if depressed and button=="l" and areas.current == "levelEditor" then
		level.saveMap()
		level.loadMap()
	end

	depressed = false
end

function drawEditorOutlines()
	--if depressed then
		local dist = distance(mStore[1], mStore[2], love.mouse.getX(), love.mouse.getY())
		love.graphics.setColor(255,0,0,140)
		if editor.tools.current == "rectangle" then
			love.graphics.rectangle("line", gridAlignX(love.mouse.getX()), gridAlignY(love.mouse.getY()),gridSize,gridSize)
		elseif editor.tools.current == "circle" then
			love.graphics.circle("line", mStore[1], mStore[2],dist,math.pi*2*dist)
		end
	--end
end

hook.Add("draw", "drawEditorOutlines", drawEditorOutlines)

function editor.testLevel()
	local p = fizz.addDynamic('rect', 250, 50, 23, 38)
	  table.insert(dynamics, p)
	  p.hasJump = false
	  p.jumpNum = 2
	  p.damping = 5
	  p.friction = 0.05
	  p.onCollide = function(p, b, nx, ny, pen)
	  fizz.testRectRect(p,b)
	    if ny < 0 then
	      p.grounded = true
	      p.jumptime = nil
	    end
	    return true
	  end

	local num
	if not #game.players then num = 1 
	else num = #game.players end
	game.players[num] = p
end

hook.Add("mousePressed", "LEMp", levelEditorMousePress)
hook.Add("mouseReleased", "LEMr", levelEditorMouseRelease)

local function paintMode()
	if depressed and areas.current == "levelEditor" and editor.paint then

		local xx = getMouseGrid()[1]
		
		local yy = getMouseGrid()[2]
		if not (level.curBaseGrid[xx] and level.curBaseGrid[xx][yy] and level.curBaseGrid[xx][yy].type) then
			if not level.curBaseGrid[xx] then level.curBaseGrid[xx] = {} end
		
			if editor.tools.current == "rectangle" then
				-- local p = fizz.addStatic('rect', gridAlignX(love.mouse.getX())+gridSize/2, gridAlignY(love.mouse.getY())+gridSize/2, gridSize, gridSize)
				-- table.insert(statics, p)
				if not level.curBaseGrid[xx][yy] then
					level.curBaseGrid[xx][yy] = {}
				end
				level.curBaseGrid[xx][yy].type = "s"
				checkSides(level.curBaseGrid)

				local loc = "maps.txt"

				--level.saveMap(loc)
				--level.loadMap(loc)

			elseif editor.tools.current == "circle" then
				-- local p = fizz.addStatic('circle', mStore[1], mStore[2], distance(mStore[1], mStore[2], love.mouse.getX(), love.mouse.getY()))
				-- table.insert(statics, p)
				if not level.curBaseGrid[xx][yy] then
					level.curBaseGrid[xx][yy] = {}
				end
				level.curBaseGrid[xx][yy].type = "c"
			end
		end
		
	end
end
hook.Add("think", "LEpaint", paintMode)